from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.urls import reverse
from .yinyue import *
import json
# Create your views here.

def music_index(request):
    m = Music()
    info_dict = m.pipei()
    context = {'info_dict':info_dict}
    return render(request,'music/index.html',context)

def kinds(request):
    
    '''
    ('/yinyue/3850056/', '<span class="HxMH album-tag"><svg class="icon icon-wanben " \
    width="36" height="18"><use xlink:href="#icon-wanben"></use></svg></span>欧美金唱盘\
    【72首经典英文歌曲】')
    '''
    
    url_id = request.GET['kinds_url'].split('/')
    
    kinds_url = 'https://www.ximalaya.com/yinyue/{}'.format(url_id[2])
    kinds_html = Music(kinds_url).html()
    head_re = '<h1 class="title">(.*?)</h1>'
    img_re = '<img src="(.*?)" alt=".*?" class="k2Sd"/>'
    music_info_re = '<a class="HxMH album-title lg" title=".*?" href="/yinyue/(.*?)/">(.*?)</a>'
    music_singer_re = '<a class="HxMH album-author" title=".*?" href=".*?">(.*?)</a>'
    head_title = re.findall(head_re,kinds_html)
    img = re.findall(img_re,kinds_html)
    music_info = re.findall(music_info_re,kinds_html)
    
    music_singer = re.findall(music_singer_re,kinds_html)
    info_dict = {}
    '''
    [('/yinyue/3595841/', '总有这样的歌只想一个人听'),....]
    '''
    for i in range(len(img)):
        info_dict[img[i]] = {'singer':music_singer[i],
                             'url':music_info[i][0],
                             'name':music_info[i][1]
                             }
    #print(info_dict)
    context = {'head_title':head_title,
               'info_dict':info_dict
               }
    
   
    return render(request,'music/kinds.html',context)


def menu(request,pk):
    '''
    https://www.ximalaya.com/revision/play/album?albumId=2650009&pageNum=1&sort=-1&pageSize=30
    page_num = info[3].replace('p','')
    url = 'https://www.ximalaya.com/revision/play/album?albumId={}&pageNum=p1&sort=-1&pageSize=30'.format(pk)
    s = requests.Session()
    json_info = s.get(url).json()
    print(json_info)
    <img class="TSFE img" src="(.*?)" alt="(.*?)"/>
    <div class="dOi2 text"><a title="(.*?)" href=".*?">.*?</a></div>
    '''
    
   
    menu_url = 'https://www.ximalaya.com/yinyue/{}/'.format(pk)
    
    menu_html = Music(menu_url).html()
    
    head_info_re = '<img class="TSFE img" src=".*?" alt="(.*?)"/>'
    menu_list_re = '<div class="dOi2 text"><a title="(.*?)" href=".*?">.*?</a></div>'
    head_info = re.findall(head_info_re,menu_html)
    #menu_list = re.findall(menu_list_re,menu_html)
    url = 'https://www.ximalaya.com/revision/play/album?albumId={}&pageNum=1&sort=-1&pageSize=30'.format(pk)
    s = requests.Session()
    res = s.get(url,headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'})
    json_info = res.json()
    audio = json_info['data']['tracksAudioPlay']
    audio_dict = {}
    for i in audio:
        m4a_url = i['src']
        name = i['trackName']
        audio_dict[name] = m4a_url
    print(audio_dict)
    context = {'name':head_info[0],
               'audio_dict':audio_dict
               }
    
    
    
    
    return render(request,'music/sing.html',context)

















